package com.ror.java.se.inet.tcp;

import java.io.*;
import java.net.Socket;

public class Demo03TcpFileMultithreadClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 10000);
        File localSrc = new File("assets"
                + File.separator + "inet"
                + File.separator + "tcp"
                + File.separator + "client-dir"
                + File.separator + "cozy.jpg");

        //读取文件数据并往服务器写出
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(localSrc));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        byte[] buf = new byte[1024];
        int len;
        while ((len = bis.read(buf)) != -1) {
            bos.write(buf, 0, len);
        }
        //写出缓冲区中剩下的数据
        bos.flush();
        //往服务器写出结束标记
        socket.shutdownOutput();
        //接收服务器的反馈
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = br.readLine();
        System.out.println(line);

        socket.close();
    }
}
